<?php

/**
 *		//adv基本函数//
 *		//这是一份试验品，由大街编写（zhongmingzi@gmail.com）
 *		//本程序默认环境：Zend Optimizer Yes / 3.3.3
 *		//		Apache/2.0.63 (Win32) PHP/5.2.12
 *		//		MySQL 支持 Yes / client lib version 5.0.89
 *		//		GD library Yes / bundled (2.0.34 compatible)
 *		//		eAccelerator/Memcache/xcache Yes
 *		//		cURL support enabled
 *		//时间：2011 4 1 04:01:07//
 */
if(!defined('IN')) {
	exit('Access Denied');
}

//检测在之后一个月内，某广告位是否有空闲
function checkadvbreak() {
	global $_G;
	$break = 3600 * 24 * 30;
	
}

function fetchadv($type = 0) {
	global $_G;
	if(!$_G['adminid']) return 0;
	$filter = $type == 0 ? '1' : " available = 0";
	$sql = "SELECT * FROM ".DB::t('ad_adv')." WHERE $filter ORDER BY advid DESC";
	$array = DB::fetch_all($sql);
	return $array;
}

function confirm($type = 0) {
	global $_G;
	if(!$_G['adminid']) return -1;
	if(!$_G['gp_advid']) return -2;
	$in = dimplode($_G['gp_advid'], 0);
	$sql = " advid IN ($in)";
	if($type == 0) return DB::update('ad_adv', array('available'=>1), $sql);
	else return DB::delete('ad_adv', $sql);
}

function getadv($typeid = 0, $xml = 0, $starttime = 0, $endtime = 0) {
	global $_G;
	$break = 3600 * 24 * 30;//一个月
	$starttime = $starttime == 0 ? TIMESTAMP - $break : $starttime;
	$endtime = $endtime == 0 ? TIMESTAMP + $break : $endtime;
	$typeid = intval($typeid);
	$sqladd = $typeid == 0 ? '' : "`type` = $typeid AND";
	$sqlbt = "BETWEEN $starttime AND $endtime";
	$sqlord = $typeid == 0 ? '' : "ORDER BY starttime";
	$token = $_G['adminid'] == 1 ? "" : "available = 1 AND";
	$sql = "SELECT * FROM ".DB::t('ad_adv')." WHERE $sqladd $token (starttime $sqlbt OR endtime $sqlbt OR (starttime < $starttime AND endtime > $endtime)) $sqlord";
	$array = DB::fetch_all($sql);
	if(!$array && $xml == 0) return false;
	if($xml != 0) {
		$now = date('r');
		$event .= <<<EVENT
			<event 
				start="$now" 
				title="现在时间">
			</event>
EVENT;
		$freest = date("r", $starttime);
		foreach($array as $k => $v) {
			$st = date("r", $v['starttime']);
			$et = date("r", $v['endtime']);
			$stc = date("m-d", $v['starttime']);
			$etc = date("m-d", $v['endtime']);
			if($typeid != 0 && $_G['adminid'] == 0) {
				if($v['starttime'] <= $starttime && $v['endtime'] < $endtime) $freest = $et;
				elseif($v['starttime'] > $starttime && $v['endtime'] >= $endtime) {
					$freeet = $st;
				} elseif($v['starttime'] > $starttime && $v['endtime'] < $endtime) {
					$freeet = $st;
					$freest_bak = $et;
				}
				if($freeet && $freest) $event .= <<<EVENT
			<event 
				start="$freest" 
				end="$freeet" 
				color="#7CCD7C"
				isDuration="true" 
				title="空闲时间 ($freest 到 $freeet)">
				本段广告时间可以购买!
			</event>
EVENT;
				if($freest_bak) $freest = $freest_bak;
			}
			$type = $typeid != 0 ? '' : "[广告位 {$v['type']}]";
			$color = $v['available'] == 1 ? '' : 'color="#FF6EB4"';
			$tips = $v['available'] == 1 ? '' : '[预订中]';
			$event .= <<<EVENT
		<event 
			start="$st" 
			end="$et" 
			$color
			link="$v[link]"
			image="$v[image]"
			isDuration="true" 
			title="$tips $type $v[title] ($stc 到 $etc)">
			<![CDATA[]]>
		</event>
EVENT;
		}
		$freeet = date("r", $endtime);
		if($typeid != 0 && $_G['adminid'] == 0 && $v['endtime'] < $endtime ) $event .= <<<EVENT
			<event 
				start="$freest" 
				end="$freeet" 
				isDuration="true" 
				color="#7CCD7C"
				title="空闲时间 ($freest 到 $freeet)">
				本段广告时间可以购买!
			</event>
EVENT;
		
		$s = <<<XML
<data 
	wiki-url="http://simile.mit.edu/shelf/" 
	wiki-section="Simile JFK Timeline">
	$event
</data>
XML;
	} else return $array;
	return $s;
}

function inputadv() {
	global $_G;
	if(!$_G['gp_advid']) return -1;
	if(!$_G['gp_starttime']) return -2;
	//if(!$_G['gp_endtime']) return -3;
	if(!$_G['gp_title']) return -4;
	if(!$_G['gp_link']) return -5;
	if(!$_G['gp_image']) return -6;
	$_G['gp_image'] = removebom($_G['gp_image']);
	$sta = explode('-', $_G['gp_starttime']);
	$st = mktime(0, 0, 0, $sta[0], $sta[1], $sta[2]);
	//$eta = explode('-', $_G['gp_endtime']);
	//$et = mktime(0, 0, 0, $eta[0], $eta[1], $eta[2]);
	$et = $st + 3600 * 24 * 30;
	if($st >= $et) return -7;
	$sql = "SELECT count(*) FROM ".DB::t('ad_adv')." WHERE type = {$_G['gp_advid']} AND available = 1  AND ((starttime > $st AND starttime < $et) OR (endtime > $st AND endtime < $et) OR (starttime <= $st AND endtime >= $et))";
	if(DB::result_first($sql)) return -8;
	
	$data = array(
		'available' => 0,
		'type' => $_G['gp_advid'],
		'title' => $_G['gp_title'],
		'link' => $_G['gp_link'],
		'image' => $_G['gp_image'],
		'tel' => $_G['gp_tel'],
		'starttime' => $st,
		'endtime' => $et,
	);
	if(DB::insert('ad_adv', $data)) return 1;
	return 0;
}